from flask import request, Blueprint
from common.log import log_handler
from algo.statistics import compute_stats

log = log_handler.LogHandler().get_log()

others = Blueprint('others', __name__)


@others.route('/statistics', methods=['POST'])
def statistics():
    params = request.form.to_dict()
    return compute_stats.begin_alg(params["source_table"], params["func"], params["feature_col"], params["method"],
                                   params["condition"], float(params["observed"]), float(params["alpha"]))


@others.route('/extract_keywords', methods=['POST'])
def extract_keywords():
    import jieba.analyse
    params = request.form.to_dict()
    sentence = params["sentence"]
    ret = jieba.analyse.extract_tags(sentence, topK=5, withWeight=True, allowPOS=())
    keywords = []
    for key, value in ret:
        if value > 0.5:
            keywords.append(key)
    return str(keywords)
